package com.chuantou.yuba.ui.widget;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chuantou.yuba.R;
import com.chuantou.yuba.api.to.IdNameTO;
import com.chuantou.yuba.util.ViewHolder;

/**
 * 周边筛选菜单
 * 
 * @author Administrator
 * 
 */
public class NearByPopup extends PopupWindow {
	private List<IdNameTO> mDataList;
	private Activity mActivity;
	private ListView mListView;
	private View mSpaceView;
	private ListAdapter mAdapter;
	private int mSelectID = 0;
	private OnMenuSelectedListener mListener;

	public NearByPopup(Activity activity, List<IdNameTO> data, int selectID) {
		this.mActivity = activity;
		this.mDataList = data;
		this.mSelectID = selectID;
		View contentView = View.inflate(activity, R.layout.near_by_popup, null);
		mListView = (ListView) contentView.findViewById(android.R.id.list);
		mSpaceView = contentView.findViewById(R.id.space_view);
		mSpaceView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
					dismiss();
				}
				return true;
			}
		});
		mListView.setCacheColorHint(Color.TRANSPARENT);
		mListView.setFadingEdgeLength(0);
		mListView.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
		mListView.setDividerHeight(activity.getResources().getDimensionPixelSize(R.dimen.divider_height));
		mListView.setDivider(mActivity.getResources().getDrawable(R.drawable.list_divider));
		mListView.setFooterDividersEnabled(true);
		mListView.setSelector(R.drawable.list_selector);
		mListView.setDividerHeight(activity.getResources().getDimensionPixelSize(R.dimen.divider_height));
		mAdapter = new ListAdapter();
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				dismiss();
				if (mListener != null) {
					IdNameTO to = (IdNameTO) parent.getItemAtPosition(position);
					mListener.onItemSelected(to);
				}
			}
		});
		setContentView(contentView);
		setHeight(FrameLayout.LayoutParams.MATCH_PARENT);
		setWidth(FrameLayout.LayoutParams.MATCH_PARENT);
		setFocusable(true);
		setAnimationStyle(R.style.popup_style);
		ColorDrawable drawable = new ColorDrawable(activity.getResources().getColor(R.color.translucence));
		setBackgroundDrawable(drawable);
	}

	public static interface OnMenuSelectedListener {
		void onItemSelected(IdNameTO to);
	}

	public void setOnMenuSelectedListener(OnMenuSelectedListener l) {
		this.mListener = l;
	}

	class ListAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mDataList == null ? 0 : mDataList.size();
		}

		@Override
		public Object getItem(int position) {
			return mDataList == null ? null : mDataList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = View.inflate(mActivity, R.layout.menu_item, null);
			}
			TextView view = ViewHolder.get(convertView, R.id.text);
			IdNameTO to = mDataList.get(position);
			if (to.getId() == mSelectID) {
				view.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.menu_item_selected, 0);
			} else {
				view.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
			}
			view.setText(to.getName());
			return convertView;
		}

	}
}
